package com.didispace;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity//开启Spring Security功能
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//重写configure方法来设置一些web安全的细节
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()//指定了了/和和“/home”不需要认证就可以访问，其他路径必须通过身份验证
                .anyRequest().authenticated()
                .and()
            .formLogin()//定义需要登陆的时候跳转到登陆界面
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }


//在内存中创建一个用户名为user 密码为password 角色为为USER的用户
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

}